package capture
/**
 * Created by IntelliJ IDEA.
 * User: szwl
 * Date: 20.09.11
 * Time: 13:54
 * To change this template use File | Settings | File Templates.
 */


import groovy.xml.*

//this class is used only for building xml messages.
public final class XmlMessageFactory {

    private final static XmlMessageFactory instance = new XmlMessageFactory()

    public static getInstance() {
        return instance
    }

    private XmlMessageFactory() {

    }

    String connect(Double server, Long vmSrvId, Long vmId) {
        StringWriter writer = new StringWriter()
        MarkupBuilder builder = new MarkupBuilder(writer)
        builder.connect(server: "$server", 'vm-server-id': "$vmSrvId", 'vm-id': "$vmId")
        return writer.toString().replace('\n', '')
    }

    String connect() {
        StringWriter writer = new StringWriter()
        MarkupBuilder builder = new MarkupBuilder(writer)
        builder.connect(server: '2.0') {}
        return writer.toString().replace('\n', '')
    }

    String pong() {
        StringWriter writer = new StringWriter()
        MarkupBuilder builder = new MarkupBuilder(writer)
        builder.pong()
        return writer.toString().replace('\n', '')
    }

    String visitEvent(VisitEventType type) {
        def options = OptionManager.getInstance()
        StringWriter writer = new StringWriter()
        MarkupBuilder builder = new MarkupBuilder(writer)
        builder.'visit-event'(
                processId: 1337, //WTF not documented
                type: type,
                time: new Date().format("d/M/yyyy H:m:s.S"),
                identifier: options.identifier,
                application: options.program,
                algorithm: 'dac', //WTF errorfix for NPE Client.java 298,304        //bulk not working
                malicious: type == VisitEventType.START ? '' : options.malicious,
                'major-error-code': type == VisitEventType.START ? '' : options.majError,
                'minor-error-code': type == VisitEventType.START ? '' : options.minError,
                visited: options.visited) {
            options.urlMap.each {k, v ->
                item(
                        url: k,
                        time: new Date().format("d/M/yyyy H:m:s.S"),
                        'major-error-code': options.majError,
                        'minor-error-code': options.minError,
                        visited: options.visited,
                        processId: 1337, //WTF not documented
                        malicious: options.malicious
                )
            }
        }
        return writer.toString().replace('\n', '')
    }


    String systemEvent(SystemEventType type, String pid, SystemEventType action, String object2, String object1) {
        def options = OptionManager.getInstance()
        StringWriter writer = new StringWriter()
        MarkupBuilder builder = new MarkupBuilder(writer)
        builder.'system-event'(
                time: new Date().format("d/M/yyyy H:m:s.S"),
                type: type, //file,process
                process: options.processMap[pid]?:object2,
                action: action,
                object1: object1=='0'?pid:object1,
                object2: object2,
                processId: pid
        )
        return writer.toString().replace('\n', '')
    }

    String option() {

    }


}

enum VisitEventType {
    START('start'),
    FINISH('finish'),
    ERROR('error')

    String desc


    VisitEventType(String desc) {

        this.desc = desc
    }

    String toString() {
        return desc
    }
}


enum SystemEventType {
    FILE('file'),
    PROCESS('process'),
    DELETE('Delete'),
    WRITE('Write'),
    OPEN('Open'),
    READ('Read'),
    CREATE('created'),
    TERMINATE('Terminate')

    String desc


    SystemEventType(String desc) {

        this.desc = desc
    }

    String toString() {
        return desc
    }
}
